package com.situ.gcbs.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.gcbs.server.dao.AdminUserDAO;
import com.situ.gcbs.server.model.AdminUser;
import com.situ.gcbs.server.model.searchbean.AdminUserSearchBean;
import com.situ.gcbs.server.service.AdminUserService;
import com.situ.gcbs.server.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2024/5/24
 * <p>
 * created by 千堆雪 on 2024/5/24 16:17, last modified by 千堆雪 on 2024/5/24 16:17
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {
    private AdminUserDAO adminUserDAO;

    @Autowired
    public void setAdminUserDAO(AdminUserDAO adminUserDAO) {
        this.adminUserDAO = adminUserDAO;
    }

    @Override
    public List<AdminUser> findAll(PaginateInfo pi, AdminUserSearchBean usb) {
        try (Page<AdminUser> page = PageHelper.startPage(pi.getPageNo(), pi.getPageSize())) {
            List<AdminUser> users = adminUserDAO.findAll(usb);

            PageInfo<AdminUser> pageInfo = new PageInfo<>(users);
            pi.setPayload(pageInfo);

            return users;
        }
    }

    @Override
    public AdminUser findByUsername(String username) {
        if (!StringUtils.hasLength(username)) {
            throw new IllegalArgumentException("用户名不可为空");
        }
        return adminUserDAO.findByUsername(username);
    }
}
